// pages/order/order.js
const requestFile = require("../../utils/request.js")
const request = requestFile.request;
const routerFile = require("../../utils/router.js");
const router = routerFile.router;
const noticeFile = require("../../utils/notice.js");
const Toast = noticeFile.Toast;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: "sh1621853078071wz",
    status: "已提交",
    applyAccount: "20198591",
    applyUser: "生活部主要负责人",
    applyDept: "生活部",
    applyDate: "2021-05-24 18:44",
    applyItems: [
      {
        id: null,
        iId: 1,
        name: "铭牌",
        num: 2,
        pic: "http://localhost:8080/static/upload/imgs/defaultPic.png",
      }
    ],
    useDate: "2021-05-24 8:00",
    returnDate: "2021-05-27 8:00",
  },

  onLoad: function (options) {
    const applyAccount = options.applyAccount;
    const applyDate = this.formatDateString(options.applyDate);
    const applyDept = options.applyDept;
    const applyItems = JSON.parse(options.applyItems);
    const applyUser = options.applyUser;
    const orderId = options.orderId;
    const returnDate = this.formatDateString(options.returnDate);
    const status = options.status;
    const useDate =this.formatDateString(options.useDate);
    this.setData({
      orderId: orderId,
      status: status,
      applyAccount: applyAccount,
      applyUser: applyUser,
      applyDept: applyDept,
      applyDate: applyDate,
      applyItems: applyItems,
      useDate: useDate,
      returnDate: returnDate,
    })
  },

  formatDateString: function(dateStr="") {
    var datePattern = /[\d]{4}-[\d]{2}-[\d]{2}/;
    var timePattern = /[\d]{2}:[\d]{2}/;
    var date = dateStr.match(datePattern)[0];
    var time = dateStr.match(timePattern)[0];
    var times = time.split(":");
    var hour = times[0];
    var calHour = parseInt(hour) + 8;
    return date + " " + calHour + ":" + times[1];
  },

  doCancel: function(e) {
    wx.navigateBack({
      delta: 1,
    })
  },
  
  doConfirm: function(e) {
    Toast.loading("订单提交中");
    const orderId = this.data.orderId;
    const userModal = wx.getStorageSync('user');
    request(router.submitOrder, "POST", {
      orderId: orderId,
      userModal: userModal,
    }).then(function(res) {
      if (res.code == 200) {
        Toast.success("提交成功")
      }
    })
    wx.navigateBack({
      delta: 1,
    })
  }
})